<template>
  <div>
      <img src="../../assets/素材_06.jpg" alt="">
    <van-nav-bar title="购物车" left-text="返回" @click-left="$router.go(-1)" />
    <div class="list">
      <div class="item" v-for="(item,index) in cartList" :key="index">
        <van-checkbox class="inp" v-model="item.checked" />
        <van-card :title="item.productName" :thumb="'/img/'+item.productImage" :price="item.salePrice" class="card">
           <!-- <template #thumb>
        <div style="width: 100%;display:flex;">
          <van-checkbox v-model="item.checked" />
          <img :src="'/img/'+item.productImage" style="width:70%" />
        </div>
      </template> -->
          <template #num>
            <van-stepper v-model="item.num" />
          </template>
        </van-card>
        <button class="shan" @click="del(index)">编辑</button>
      </div>
    </div>
    <input type="checkbox" :checked="allCheck" @click="quan">全选</input>
    <van-submit-bar :price="tonums" button-text="提交订单" style="bottom:1rem;">
      <!-- <van-checkbox :checked="allCheck" @click="quan">全选</van-checkbox> -->
    </van-submit-bar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cartList: [],
      // checked: true,
      allCheck: false,
    };
  },  
  mounted() {
    this.cartList = this.$store.state.m_cartList;
  },
  created() {},
  computed: {
    tonums() {
      //数据的提交
      if (this.cartList.length > 0) {
        this.$store.commit("addCart", this.cartList);
      }
      let amounts = 0;
      this.cartList.forEach((item) => {
        if (item.checked == true) {
          amounts += item.num * item.salePrice;
        }
      });
      return amounts * 100;
    },
  },
  watch: {
    cartList: {
      handler(val) {
        this.allCheck = this.cartList.every((item) => {
          return item.checked;
        });
      },
      deep: true,
    },
  },

  methods: {
    del(index) {
      this.cartList.splice(index, 1);
    },
    quan() {
      //先取反
      this.allCheck = !this.allCheck;
      this.cartList.forEach((item) => {
        item.checked = this.allCheck;
      });
    },
  },
};
</script>

<style scoped lang="less">
.list {
  width: 100%;
  .item {
    padding: 0.1rem;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    align-items: center;
    .inp {
      width: 15%;
    }
    .card {
      flex: 1;
    }
  }
}
.shan {
  font-size: 0.14rem;
}
</style>






